package com.zhangzeqin.code.view;

import com.zhangzeqin.code.controller.UserController;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.SQLException;

public class StudyView extends JFrame {

    JLabel nameLabel=new JLabel("学习记录",JLabel.CENTER);
    SpringLayout springLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(springLayout);
    JLabel clockLabel=new JLabel("打卡天数:");
    JTextField clockTxt=new JTextField();
    JLabel timeLabel=new JLabel("学习时长:");
    JTextField timeField=new JTextField();
    JLabel wordlabel=new JLabel("已学词数:");
    JTextField wordField=new JTextField();
    JLabel aimlabel=new JLabel("目标词数:");
    JTextField aimField=new JTextField();
    JButton rankButton=new JButton("排行榜");
    JButton returnButton=new JButton("返回");
    JButton modifyButton=new JButton("修改");

    /**
     * 数组接收文本框值
     */
    Object[] arr=null;
    /**
     * 控制层对象
     */
    UserController userController=new UserController();

    public static void main(String[] args) {
        new StudyView();
    }
    public StudyView(){
        super("学习记录");
        //获取内容面板
        Container contentPane=getContentPane();
        nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        nameLabel.setPreferredSize(new Dimension(0,80));
        //设置字体大小
        Font  centerFont=new Font("楷体",Font.PLAIN,20);
        clockLabel.setFont(centerFont);
        timeLabel.setFont(centerFont);
        wordlabel.setFont(centerFont);
        aimlabel.setFont(centerFont);
        rankButton.setFont(centerFont);
        returnButton.setFont(centerFont);
        modifyButton.setFont(centerFont);
        //设置文本框大小
        clockTxt.setPreferredSize(new Dimension(80,30));
        timeField.setPreferredSize(new Dimension(80,30));
        wordField.setPreferredSize(new Dimension(80,30));
        aimField.setPreferredSize(new Dimension(80,30));
        //将组件添入面板
        centerPanel.add(clockLabel);
        centerPanel.add(clockTxt);
        centerPanel.add(timeLabel);
        centerPanel.add(timeField);
        centerPanel.add(wordlabel);
        centerPanel.add(wordField);
        centerPanel.add(aimlabel);
        centerPanel.add(aimField);
        centerPanel.add(rankButton);
        centerPanel.add(returnButton);
        centerPanel.add(modifyButton);

        //弹簧布局
        layout(contentPane);

        //初始化个文本框
        initialize();

        //监听器
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new WordBookView();
            }
        });
        rankButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                newJFrame();
            }
        });
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (aimField.getText().length() == 0) {
                    JOptionPane.showMessageDialog(null, "不能为空！");
                } else {
                    String aimWord = aimField.getText();
                    boolean flag = userController.modifyPlan(aimWord);
                    if (flag) {
                        JOptionPane.showMessageDialog(null, "修改成功！");
                    }
                }
            }
        });
        //自定义图标
        URL imgUrl=LoginView.class.getClassLoader().getResource("image.png");
        setIconImage(new ImageIcon(imgUrl).getImage());

        //窗口大小
        setSize(600,400);
        //计算位置居中
        setLocationRelativeTo(null);
        //关闭退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        //可视
        setVisible(true);
    }

    private void initialize() {
        //文本框初始化
        try {
            arr=userController.pInformation();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        clockTxt.setText( arr[4].toString());
        timeField.setText( arr[5].toString());
        wordField.setText( arr[6].toString());
        aimField.setText(arr[7].toString());
        clockTxt.setEditable(false);
        timeField.setEditable(false);
        wordField.setEditable(false);
    }

    /**
     * 创建一个子窗口
     */
    public static void newJFrame(){
        /**
         * 控制层对象
         */
        UserController userController=new UserController();
        JFrame rankJFrame =new JFrame("学习排行榜");
        //设置窗口大小,不可修改大小
        rankJFrame.setSize(400,400);
        rankJFrame.setResizable(false);
        //关闭窗口时,不结束程序
        rankJFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //计算位置居中
        rankJFrame.setLocationRelativeTo(null);
        //添加组件
        JPanel jPanel=new JPanel(new GridLayout(1, 1));

        //添加表格
        //表头
        Object [] columnNames={"姓名","已学词数"};
        Object[][]rowData=userController.rankUser();
        //创建一个表格,指定表头和所有行数据
        JTable table = new JTable(rowData, columnNames);
        //tablemodel:和jtable关联后,更新model就能把数据的变化反应到jtable
        DefaultTableModel tableModel=new DefaultTableModel();
        tableModel.setDataVector(rowData,columnNames);
        //jtable与table关联
        JTable jTable=new JTable(tableModel);
        jPanel.add(table);
        rankJFrame.setContentPane(jPanel);
        rankJFrame.setVisible(true);
    }
    /**
     * 弹簧布局
     * @param contentPane
     */
    private void layout(Container contentPane) {
        //布局clockLabel
        Spring childWidth=Spring.sum(Spring.sum(Spring.width(clockLabel),Spring.width(clockTxt)),
                Spring.constant(20));
        int offSetX=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,clockLabel,-offSetX,
                SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,clockLabel,20,SpringLayout.NORTH,centerPanel);
        //clockTxt
        springLayout.putConstraint(SpringLayout.WEST,clockTxt,20,SpringLayout.EAST,clockLabel);
        springLayout.putConstraint(SpringLayout.NORTH,clockTxt,0,SpringLayout.NORTH,clockLabel);
        //timeLabel
        springLayout.putConstraint(SpringLayout.EAST,timeLabel,0,SpringLayout.EAST,clockLabel);
        springLayout.putConstraint(SpringLayout.NORTH,timeLabel,20,SpringLayout.SOUTH,clockLabel);
        //timeField
        springLayout.putConstraint(SpringLayout.WEST,timeField,20,SpringLayout.EAST,timeLabel);
        springLayout.putConstraint(SpringLayout.NORTH,timeField,0,SpringLayout.NORTH,timeLabel);
        //wordlabel
        springLayout.putConstraint(SpringLayout.EAST,wordlabel,0,SpringLayout.EAST,timeLabel);
        springLayout.putConstraint(SpringLayout.NORTH,wordlabel,20,SpringLayout.SOUTH,timeLabel);
        //wordField
        springLayout.putConstraint(SpringLayout.WEST,wordField,20,SpringLayout.EAST,wordlabel);
        springLayout.putConstraint(SpringLayout.NORTH,wordField,0,SpringLayout.NORTH,wordlabel);
        //aimlabel
        springLayout.putConstraint(SpringLayout.EAST,aimlabel,0,SpringLayout.EAST,wordlabel);
        springLayout.putConstraint(SpringLayout.NORTH,aimlabel,20,SpringLayout.SOUTH,wordlabel);
        //aimField
        springLayout.putConstraint(SpringLayout.WEST,aimField,20,SpringLayout.EAST,aimlabel);
        springLayout.putConstraint(SpringLayout.NORTH,aimField,0,SpringLayout.NORTH,aimlabel);
        //rankButton
        springLayout.putConstraint(SpringLayout.WEST,rankButton,0,SpringLayout.WEST, wordlabel);
        springLayout.putConstraint(SpringLayout.NORTH,rankButton,80,SpringLayout.SOUTH,wordlabel);
        //returnButton
        springLayout.putConstraint(SpringLayout.WEST,returnButton,50,SpringLayout.EAST, rankButton);
        springLayout.putConstraint(SpringLayout.NORTH,returnButton,0,SpringLayout.NORTH,rankButton);
        //modifyButton
        springLayout.putConstraint(SpringLayout.WEST,modifyButton,50,SpringLayout.EAST,aimField);
        springLayout.putConstraint(SpringLayout.NORTH,modifyButton,0,SpringLayout.NORTH,aimField);

        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
    }
}
